/*
 * Copyright (c) 2004-2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gou Pengfei
 * Date:    
 */

#ifndef __CPU_EDGE_HISTORY_TRACKER_HH__
#define __CPU_EDGE_HISTORY_TRACKER_HH__

#include <vector>
#include <deque>

#include "base/misc.hh"
#include "base/types.hh"
#include "config/the_isa.hh"
#include "arch/isa_traits.hh"
#include "cpu/edge/pred/base_predictor.hh"

template<class Impl>
class HistoryTracker : public BasePredictor<Impl>
{
  public:
    typedef TheISA::BlockID BlockID;
    typedef TheISA::BTypeID BTypeID;
    typedef TheISA::ExitID ExitID;
    typedef std::deque<Addr> PathHistory;

    HistoryTracker(DerivEdgeCPUParams *params);

    ~HistoryTracker();

    void reset();	

    const std::string &name() const { return _name; }

    /**
     * Lookup the history tracker to store the newly updated histories for
     * exit ids, types and binary histories. 
     */
    void lookup(Addr block_pc, BlockID block_id, ExitID pred_exit_id,
            BTypeID pred_type_id, uint64_t predication_pattern, ThreadID tid);

    /**
     * Update the histories with actual exit ids, types and binary histories.
     */
    void update(Addr block_pc, BlockID block_id, ExitID actual_exit_id,
            BTypeID actual_type_id, ThreadID tid);

    /**
     * Restores the histories with actual exit ids, types and binary
     * histories.
     */
    void squash(Addr block_pc, BlockID block_id, ExitID actual_exit_id, 
            BTypeID actual_type_id, ThreadID tid);

    /**
     * Restores the global branch history on a squash.
     */
    void squash(BlockID block_id, ThreadID tid);

    uint64_t getExitHistory(ThreadID tid) { return globalExitHistory[tid]; }

    uint64_t getExitHistory(BlockID block_id, ThreadID tid);

    uint64_t getExitHistory(BlockID block_id, ThreadID tid, ExitID exit_id);

    uint64_t getTypeHistory(ThreadID tid) { return globalTypeHistory[tid]; }

    uint64_t getTypeHistory(BlockID block_id, ThreadID tid);

    uint64_t getTypeHistory(BlockID block_id, ThreadID tid, BTypeID type_id);

    uint64_t getBinaryHistory(ThreadID tid) { return globalBinaryHistory[tid]; }

    uint64_t getBinaryHistory(BlockID block_id, ThreadID tid);

    uint64_t getBinaryHistory(BlockID block_id, ThreadID tid, BTypeID type_id);

    uint64_t getPathHistory(ThreadID tid);

    uint64_t getPathHistory(BlockID block_id, ThreadID tid);

    uint64_t getPathHistory(BlockID block_id, ThreadID tid, Addr pc);

    uint64_t getPredicationPattern(BlockID block_id, ThreadID tid);

    int getCounterBits() { panic("Unimplemented function: getCounterBits().\n"); }

  private:

    const std::string _name;

    /** 
     * Checkpoint entry for each speculative block.
     * @TODO Add local history to this entry.
     * */
    struct CheckpointEntry {
        CheckpointEntry () 
            : blockID(0),
            globalExitHistory(0),
            globalTypeHistory(0),
            globalBinaryHistory(0)
        {
        }

        void reset()
        {
            blockID = 0;
            globalExitHistory = 0;
            globalTypeHistory = 0;
            globalBinaryHistory = 0;

            for (int i = 0; i < pathHistory.size(); ++i) {
                pathHistory[i] = 0;
            }
        }

        BlockID blockID;
        uint64_t globalExitHistory;
        uint64_t globalTypeHistory;
        uint64_t globalBinaryHistory;
        PathHistory pathHistory;

        uint64_t predicationPattern;
    };

    /** Speculative checkpoints. */
    CheckpointEntry checkpointEntries[Impl::MaxInFlightBlockNum];

    void initVars();

    void updatePathHistory(PathHistory &path_history, Addr new_pc);

    /** Global exit id history register. */
    uint64_t globalExitHistory[Impl::MaxThreads];

    /** Global exit type history register. */
    uint64_t globalTypeHistory[Impl::MaxThreads];

    /** Global binary history register. */
    uint64_t globalBinaryHistory[Impl::MaxThreads];

    /** Path histories of all threads, speculatively updated. */
    PathHistory pathHistoryTable[Impl::MaxThreads];

    /** Number of bits for the global history. */
    uint64_t globalHistoryBits;

    /** Mask to get the proper global history. */
    uint64_t globalHistoryMask;

    /** Number of bits to shift the instruction over to get rid of the word
     *  offset.
     */
    uint64_t blockShiftAmt;

    /** Type id bits. */
    int typeIdBits;

    /** Type id mask. */
    uint64_t typeIdMask;

    /** Exit id bits. */
    int exitIdBits;

    /** Exit id mask. */
    uint64_t exitIdMask;
};


#endif //__CPU_EDGE_HISTORY_TRACKER_HH__
